/**
 * 获取token
 */
import * as _myTool from './tool.js'
function paramFromUrl(name) {
  var reg = new RegExp('(^|&|#)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.href.replace(/(.*)(\?)(.*)/, '$3').match(reg)
  return r ? r[2] : ''
}
function getTokenM(type) {
  let myToken = ''
  let baseURL = ''
  switch (process.env.NODE_ENV) {
    case 'development':
      // 开发环境配置
      myToken = _myTool._getLocalItem('jx_Authorization')
      if (myToken) {
        _myTool._setLocalItem('jx_Authorization', myToken)
      }
      baseURL = '' // 测试生产环境
      break
    case 'production':
      // 生产环境配置
      baseURL = document.location.origin
      break
  }

  // 测试token时 注释下面代码if else
  if (paramFromUrl('token')) {
    // 免登登录判断
    _myTool._setLocalItem('jx_Authorization', decodeURIComponent(paramFromUrl('token')))
    _myTool._setLocalItem('refresh_Authorization', decodeURIComponent(paramFromUrl('jx_refresh_token')))
    if (_myTool._getLocalItem('jx_Authorization') == null) {
      myToken == ''
    } else {
      myToken = _myTool._getLocalItem('jx_Authorization')
    }
  } else {
    // 帐号密码登录判断
    myToken = _myTool._getLocalItem('jx_Authorization') ? _myTool._getLocalItem('jx_Authorization') : ''
  }
  if (type == 'baseURL') {
    return baseURL
  } else {
    return myToken
  }
}
export { getTokenM }
